<?php
/*********************************************
  Copyright (c) 2011 by DragonflyCMS dev team
  http://dragonflycms.org
  Released under GNU GPL version 2 or any later version
**********************************************/

/**
* title: Spam class - BETA
* requires: Net Client
*/

abstract class Spam
{

	/* enable logging */
	public static $log = true;

	/* specify the file to log to, full or relative path */
	public static $logFile = 'spam.log';

	/* Search setup */

	/* Increase chances with long words */
	public static $wordLength = 15;

	/* Dictionary word list */
	//public static $dictionary = 'viagra|marketing|dexasone|loxitane|aciphexz|vibramycin|amoxocillin|nasonex|cephalexin|hot|porno|viagra|sex|marketing';
	public static $dictionary = array('viagra','marketing','generic','discount','free');

	/* If any of the aboce $search* directly fails, sum up the count of each occurences */
	public static $max = 3;

	/* time format used in logs */
	public static $dateFormat = 'd-M-Y,g:ia';

	/**
	 * return: bool
	 */
	public static function exists($text)
	{
		$text = mb_strtolower($text);
		$_word_list = mb_split('\W', $text);
		$c = 0;
		foreach ($_word_list as $match) {
			$l = mb_strlen($match);
			if (4 > $l) { continue; }
			if (false !== strpos($match, 'http') || self::$wordLength < $l || in_array($match, self::$dictionary)) { ++$c; }
		}
		if ($c > self::$max) { if (self::$log) { self::log($text); } return true; }
		return false;
	}

	private function log($text)
	{
		trigger_error(date(self::$dateFormat) .' ' .Dragonfly_Net::ip() ." ". Dragonfly::getKernel()->IDENTITY->nickname .":\n{$text}\n###############\n", E_USER_WARNING);
	}
}
